package Test;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.LockSupport;

class Test {
    AtomicInteger atomicInteger = new AtomicInteger();
    public void add() {
        atomicInteger.getAndIncrement();
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("这是遗言");
    }
}

public class TestDemo12 {
    public static void main(String[] args) throws InterruptedException {
        WeakReference<Test> testWeakReference = new WeakReference<>(new Test());
        System.out.println("gou"+testWeakReference.get());
        System.gc();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("被清了"+testWeakReference.get());
    }
    public static void main6(String[] args) throws InterruptedException {
        SoftReference<Test> testSoftReference = new SoftReference<>(new Test());
        System.gc();
        TimeUnit.SECONDS.sleep(2);
        System.out.println(testSoftReference.get());
        try {
            byte[] bytes = new byte[20 * 1024 * 1024];
        } finally {
            System.out.println("不够用"+testSoftReference.get() );
        }
    }
    public static void main5(String[] args) throws InterruptedException {
        Test test = new Test();
        System.out.println(test);
        test = null;
        System.out.println("要开始清理了");
        System.gc();
        TimeUnit.MILLISECONDS.sleep(500);
        System.out.println("清理完成");
    }
    public static void main4(String[] args) {
        ThreadLocal<Integer> integerThreadLocal = ThreadLocal.withInitial(()->0);
        integerThreadLocal.set(1+integerThreadLocal.get());
        System.out.println(integerThreadLocal.get());
    }
    public static void main3(String[] args) {
        AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100,1);
        atomicStampedReference.compareAndSet(100,101,1,2);
        atomicStampedReference.compareAndSet(101,100,2,3);
        System.out.println(atomicStampedReference.compareAndSet(100, 101, 1, 2));
        System.out.println(atomicStampedReference.getStamp());
    }
    public static void main2(String[] args) throws InterruptedException {
        Test test = new Test();
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for (int i = 0; i < 50; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int j = 0; j < 1000; j++) {
                            test.add();
                        }
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            }).start();
        }
        countDownLatch.await();
        System.out.println(test.atomicInteger.get());
    }
    public static void main1(String[] args) {
        AtomicReference<Integer> atomicReference = new AtomicReference<>();
        Integer a = 10;
        Integer b = 20;
        atomicReference.set(a);
        System.out.println(atomicReference.compareAndSet(a, b)+"  "+atomicReference.get());
        System.out.println(atomicReference.compareAndSet(a, b)+"  "+atomicReference.get());
        LongAdder longAdder = new LongAdder();
        longAdder.add(1);
        System.out.println(longAdder.toString());

    }
}
